package study.spring.core.aop;

import java.beans.Beans;
import java.time.LocalDateTime;
import java.time.LocalTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 演示 spring 使用 AspectJ 实现 AOP, 包括 AOP实例为  单例、原型的两种情况
 */
@ComponentScan(basePackages = "study.spring.core.aop")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringAopConfig {
 private static Logger log = LoggerFactory.getLogger(SpringAopConfig.class);

  public static void main(String[] args) {
    // AspectInstantiationModels();
    testAop();
  }

  public static void AspectInstantiationModels() {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringAopConfig.class);
    TargetObjectPrototype prototype = ctx.getBean(TargetObjectPrototype.class);
    TargetObjectPrototype prototype2 = ctx.getBean(TargetObjectPrototype.class);
    System.out.println("单例 aspect bean 演示: ");
    prototype.singletonAspectUse();
    prototype2.singletonAspectUse();
    System.out.println("原型 aspect bean 演示: ");
    prototype.prototypeAspectUse();
    prototype2.prototypeAspectUse();
  }

  public static void testAop() {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringAopConfig.class);
    TargetObject targetObject = ctx.getBean(TargetObject.class);
    log.info("代理后的 class: {}", targetObject.getClass().getName());
    targetObject.joinPoint();
    targetObject.joinPoint2("调用时传的参数", LocalTime.now());
  }
}
